# -*- coding: utf-8 -*-

import json
import datetime
import decimal
from bson import ObjectId

'''
    把数据库查询到的数据转换为json格式的工具类
'''


class MyJsonEncoder(json.JSONEncoder):
    '''
        重写构造json类，对日期等格式进行特殊处理
    '''

    def default(self, obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, datetime.date):
            return obj.strftime('%Y-%m-%d')
        elif isinstance(obj, decimal.Decimal):
            return str(obj)
        elif isinstance(obj, ObjectId):
            return str(obj)
        else:
            return json.JSONEncoder.default(self, obj)

class Table2JsonZhao(object):
    @staticmethod
    def view_favor_cart_item_list_to_json(lists):
        data = []
        for row in lists:
            result = {}
            result['id'] = row[0]
            result['userId'] = row[1]
            result['isLock'] = row[2]
            result['itemId'] = row[3]
            result['deptId'] = row[4]
            result['itemHandlername'] = row[5]
            result['itemHandlerphone'] = row[6]
            result['itemTitle'] = row[7]
            result['itemPricerange'] = row[8]
            result['itemContent'] = row[9]
            result['itemUrl'] = row[10]
            result['itemDeadline'] = row[11]
            result['itemPublishdate'] = row[12]
            result['itemAddress'] = row[13]
            result['itemStatus'] = row[14]
            result['serviceId']=row[15]
            result['itemEnterPerson']=row[16]
            result['itemEnterTime']=row[17]
            result['checkStatusOne']=row[18]
            result['checkRemarkOne']=row[19]
            result['checkPersonOne']=row[20]
            result['checkTimeOne']=row[21]
            result['checkStatusTwo']=row[22]
            result['checkRemarkTwo']=row[23]
            result['checkPersonTwo']=row[24]
            result['checkTimeTwo']=row[25]
            result['deptName'] = row[26]
            result['deptPcode'] = row[27]
            result['deptCode'] = row[28]
            result['deptIsLock'] = row[29]
            result['deptSort'] = row[30]
            result['deptAddress'] = row[31]
            result['leveCode'] = row[32]
            result['provinceId'] = row[33]
            result['cityId'] = row[34]
            result['districtId'] = row[35]
            result['deptUrl'] = row[36]
            result['desUrl'] = row[37]
            result['extractFrom'] = row[38]
            result['extractDate'] = row[39]


            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)

        return jsonData

    @staticmethod
    def view_category_item_list_to_json(lists):
        data = []
        for row in lists:
            result = {}
            result['itemId'] = row[0]
            result['deptId'] = row[1]
            result['itemHandlername'] = row[2]
            result['itemHandlerphone'] = row[3]
            result['itemTitle'] = row[4]
            result['itemPricerange'] = row[5]
            result['itemContent'] = row[6]
            result['itemUrl'] = row[7]
            result['itemDeadline'] = row[8]
            result['itemPublishdate'] = row[9]
            result['itemAddress'] = row[10]
            result['itemStatus'] = row[11]
            result['serviceId'] = row[12]
            result['itemEnterPerson'] = row[13]
            result['itemEnterTime'] = row[14]
            result['checkStatusOne'] = row[15]
            result['checkRemarkOne'] = row[16]
            result['checkPersonOne'] = row[17]
            result['checkTimeOne'] = row[18]
            result['checkStatusTwo'] = row[19]
            result['checkRemarkTwo'] = row[20]
            result['checkPersonTwo'] = row[21]
            result['checkTimeTwo'] = row[22]
            result['id'] = row[23]
            result['deptName'] = row[24]
            result['deptPcode'] = row[25]
            result['deptCode'] = row[26]
            result['deptSort'] = row[27]
            result['deptAddress'] = row[28]
            result['leveCode'] = row[29]
            result['provinceId'] = row[30]
            result['cityId'] = row[31]
            result['districtId'] = row[32]
            result['deptUrl'] = row[33]
            result['desUrl'] = row[34]
            result['levelName'] = row[35]
            result['provinceName'] = row[36]
            result['cityName'] = row[37]
            result['districtName'] = row[38]
            result['catId'] = row[39]
            result['catName'] = row[40]
            result['catPcode'] = row[41]
            result['catCode'] = row[42]
            result['catStatus'] = row[43]
            result['catSort'] = row[44]
            result['deptIsLock'] = row[45]

            data.append(result)
        jsonData = json.dumps(data, cls=MyJsonEncoder, ensure_ascii=False)

        return jsonData


